import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';

import {
  	AbstractControl,
  	FormBuilder,
	FormGroup,
  	Validators
} from '@angular/forms';
import {LoginService } from '../service/login.service';
import { ToastrService } from 'ngx-toastr';
import {Login} from '../service/login'
@Component({
  selector: 'app-login',
  templateUrl: './login.component.html',
  styleUrls: ['./login.component.less']
})

export class LoginComponent implements OnInit {
	validateForm: FormGroup;
	selectedLogin : Login;
	logins : Login [];
  	constructor(
  		private fb: FormBuilder,
  		private loginService:LoginService,
  		private toastr: ToastrService,
  		private router:Router) { 
  	}
	submitForm(): void {
	    for (const i in this.validateForm.controls) {
	      this.validateForm.controls[ i ].markAsDirty();
	      this.validateForm.controls[ i ].updateValueAndValidity();
	    }
	    //验证登陆接口
	    this.toastr.error('everything is broken', 'Major Error', {
		  timeOut: 3000
		});
	    if(this.validateForm.valid){
	    	this.getLogins();
	    	
	    	//this.router.navigate(['dashboard']);
	    }
	    
  	}
  	ngOnInit(): void {
	    this.validateForm = this.fb.group({
	      userName: [ null, [ Validators.required ] ],
	      password: [ null, [ Validators.required ] ],
	      remember: [ true ]
	    });
  	}
  	onSelect(login:Login):void{
  		this.selectedLogin = login;
  	}
  	getLogins():void{
  		 this.loginService.getLogins()
  		.subscribe(logins=>this.logins= logins);
  	}

}
